package cn.com.shopec.erp.financing.controllor;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.shopec.erp.common.excel.ExcelBaseExport;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.financing.entity.SupplierSettlement;
import cn.com.shopec.erp.financing.service.ISupplierSettlementService;
import cn.com.shopec.erp.financing.vo.SupplierSettlementVo;
import cn.com.shopec.erp.stock.excel.impl.SupplierSettlementExcelExport;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.vo.UserVo;

@Controller
@RequestMapping("/financing/settlement")
public class SupplierSettlementController {

	@Resource
	private ISupplierSettlementService supplierSettlementService;

	private static final Log log = LogFactory.getLog(SupplierSettlementController.class);

	@RequestMapping("toSupplierSettlement")
	public String toSupplierSettlement() {
		return "financing/supplierSettlement";
	}

	@RequestMapping("toSupplierSettlementDetail")
	public String toSupplierSettlementDetail(String settlementNo, ModelMap map) {
		map.put("settlementNo", settlementNo);
		return "financing/supplierSettlementDetail";
	}

	@RequestMapping("toCheckSupplierSettlement")
	public String toCheckSupplierSettlement(String settlementId,String settlementNo, ModelMap map) {
		map.put("settlementId", settlementId);
		map.put("settlementNo", settlementNo);
		return "financing/supplierSettlementCheck";
	}

	@RequestMapping("supplierSettlementPageList")
	@ResponseBody
	public String supplierSettlementPageList(SupplierSettlementVo supplierSettlementVo, PageQuery pageQuery) {
		PageFinder<SupplierSettlement> page = supplierSettlementService.supplierSettlementPageList(supplierSettlementVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}
	
	/**
	 * 导出EXCEL
	 * @param supplierSettlementVo
	 * @param response
	 */
	@RequestMapping("exportSupplierSettlement")
	public void exportSupplierSettlement(SupplierSettlementVo supplierSettlementVo, HttpServletResponse response){
		PageFinder<SupplierSettlement> page = supplierSettlementService.supplierSettlementPageList(supplierSettlementVo, null);
		ExcelBaseExport excelBaseExport = new SupplierSettlementExcelExport(page.getData(), "供应商结算信息_"+System.currentTimeMillis(), response);
		try {
			excelBaseExport.wirteExcel();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("supplierSettlementDetailPageList")
	@ResponseBody
	public String supplierSettlementDetailPageList(SupplierSettlementVo supplierSettlementVo, PageQuery pageQuery) {
		PageFinder page = supplierSettlementService.supplierSettlementDetailPageList(supplierSettlementVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}

	@RequestMapping("createSupplierSettlement")
	@ResponseBody
	public String createSupplierSettlement() {
		Map<String, String> map = DateUtil.getStartAndEndThree(30);
		return supplierSettlementService.createSupplierSettlement(map.get("start"), map.get("end"));
	}

	@RequestMapping("checkSupplierSettlement")
	@ResponseBody
	public String checkSupplierSettlement(SupplierSettlementVo supplierSettlementVo) {
		UserVo userVo = UserUtils.getUserVo();
		supplierSettlementVo.setOperatorId(userVo.getId());
		return supplierSettlementService.checkSupplierSettlement(supplierSettlementVo);
	}

}
